#include "mapwindow.h"
#include "ui_mapwindow.h"
#include "common.h"

#include <QNetworkAccessManager>
#include <QJsonDocument>
#include <QJsonObject>
#include <QPixmap>
#include <QTouchEvent>

MapWindow::MapWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MapWindow)
{
    ui->setupUi(this);

    //调用函数
    initMap();  //初始化地图

    //设置触摸事件
    setAttribute(Qt::WA_AcceptTouchEvents);

    //安装事件过滤器
    this->installEventFilter(this);
}

MapWindow::~MapWindow()
{
    delete ui;
}

//初始化地图函数
void MapWindow::initMap()
{
    initLngLat();  //调用静态图前先获取经纬度

    QNetworkAccessManager *manager=new QNetworkAccessManager();
    QString mapURL=QString(MAP_URL).arg(MAP_AK).arg(this->zoom).arg(this->lng).arg(this->lat);

    this->reply=manager->get(QNetworkRequest(QUrl(mapURL)));

    file=new QFile();
    file->setFileName("./temp.png");
    file->open(QFile::WriteOnly);

    connect(reply,&QNetworkReply::readyRead,this,&MapWindow::readMapData);
    connect(reply,&QNetworkReply::finished,this,&MapWindow::showMap);
}

//初始化经纬度函数
void MapWindow::initLngLat()
{
    QNetworkAccessManager *manager=new QNetworkAccessManager();
    QString mapURL=QString(MAP_URL_IP).arg(MAP_IP).arg(MAP_AK);

    manager->get(QNetworkRequest(QUrl(mapURL)));

    connect(manager,&QNetworkAccessManager::finished,this,&MapWindow::getLngLat);
}

//拖动地图事件过滤器
bool MapWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == this ){

            QTouchEvent* touch = static_cast<QTouchEvent*>(event);
            switch (event->type()) {
            case QEvent::MouseButtonPress://鼠标按下事件

                pres_flag = 1;

                last_point.setY(cursor().pos().y());     // 记录按下点的y坐标
                last_point.setX(cursor().pos().x());     // 记录按下点的x坐标
                break;
            case QEvent::MouseButtonRelease://鼠标松开事件
                rele_flag = 1;
                point.setY(cursor().pos().y());     // 记录按下点的y坐标
                point.setX(cursor().pos().x());     // 记录按下点的x坐标
                break;

            case QEvent::TouchBegin://触摸事件

                point.setY(touch->touchPoints().at(0).pos().y());     // 记录按下点的y坐标
                point.setX(touch->touchPoints().at(0).pos().x());     // 记录按下点的x坐标
                if(point.x()<580 || point.y()<350)
                {
                    rele_flag = 1;
                    pres_flag = 1;
                }
                event->accept();
                break;

            default:
                break;
            }
        }

        int m_x;
        int m_y;
        double move_x;
        double move_y;
        if(pres_flag==1 && rele_flag==1)
        {
            pres_flag = 0;
            rele_flag = 0;
            if(event->type() == QEvent::TouchBegin)//触摸
            {
                m_x = 400-point.x();
                m_y = 240-point.y();
            }
            else {    //鼠标
                m_x = point.x()-last_point.x();
                m_y = point.y()-last_point.y();
            }
            if(m_x>10 || m_x<-10 || m_y>10 || m_y<-10)
            {
                int m_zoom = zoom;
                //计算需要偏移多少东经北纬
                move_x = m_x*0.0000003*(19-m_zoom)*(19-m_zoom)*(19-m_zoom)*(19-m_zoom);
                move_y = m_y*0.0000003*(19-m_zoom)*(19-m_zoom)*(19-m_zoom)*(19-m_zoom);
                double east=this->lng.toDouble();
                double north=this->lat.toDouble();
                east -= move_x;
                north += move_y;
                this->lng=QString::number(east);
                this->lat=QString::number(north);

                initMap();//发送图片请求
            }

        }

        return QWidget::eventFilter(watched,event);//将事件传递给父类
}

//返回主页面按钮槽函数
void MapWindow::on_button_back_clicked()
{
    this->hide();
    this->parentWidget()->show();
}

//获取经纬度槽函数
void MapWindow::getLngLat(QNetworkReply *reply)
{
    QJsonDocument doc=QJsonDocument::fromJson(reply->readAll());
    QJsonObject obj=doc.object();

    QJsonObject point=obj.value("content").toObject().value("point").toObject();
    this->lng=point.value("x").toString();
    this->lat=point.value("y").toString();
}

//展示图片槽函数
void MapWindow::showMap()
{
    file->close();

    QPixmap map = QPixmap("./temp.png");
    ui->label_map->setPixmap(map);
}

//读取图片数据槽函数
void MapWindow::readMapData()
{
    file->write( reply->readAll());
}

//放大地图按钮槽函数
void MapWindow::on_button_big_clicked()
{
    this->zoom++;
    initMap();
}

//缩小地图按钮槽函数
void MapWindow::on_button_small_clicked()
{
    this->zoom--;
    initMap();
}

